package com.sj.parksdk.fragment.profile;

import android.annotation.SuppressLint;

import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.core.http.subscriber.TipProgressLoadingSubscriber;
import com.sj.parksdk.net.base.CommonResult;
import com.sj.parksdk.net.http.ApiHelper;
import com.sj.parksdk.utils.TokenUtils;
import com.sj.parksdk.utils.XToastUtils;
import com.sj.parksdk.widget.sweetalert.SweetAlertDialog;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;
import com.xuexiang.xutil.common.StringUtils;

import butterknife.BindView;
@Page(name = "设置")
public class FragmentSetting extends BaseFragment implements SuperTextView.OnSuperTextViewClickListener {
    @BindView(R2.id.menu_setting)
    SuperTextView menuSetting;
    @BindView(R2.id.menu_logout)
    SuperTextView menuLogout;

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_fragment_setting;
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initListeners() {
        menuSetting.setOnSuperTextViewClickListener(this);
        menuLogout.setOnSuperTextViewClickListener(this);
    }

    private void showLoginOutDialog() {
        new SweetAlertDialog(getContext(),SweetAlertDialog.NORMAL_CENTER_TITLE_TYPE)
                .setContentMinHeight(DensityUtils.dp2px(100))
                .setContentText("确定要退出登录吗?")
                .setCancelClickListener(null)
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        loginOut();
                    }
                }).show();
    }

    @SuppressLint("CheckResult")
    private void loginOut() {
        CustomRequest request = XHttp.custom();
        request.call(ApiHelper.getInstance(request).logout()).subscribeWith(new TipProgressLoadingSubscriber<CommonResult>() {
            @Override
            protected void onSuccess(CommonResult result) {
                if(result.getCode() == 200) {
                    TokenUtils.handleLogoutSuccess();
                    popToBack();
                }else{
                    XToastUtils.error(StringUtils.isEmpty(result.getMessage())?"请求失败":result.getMessage());
                }
            }
        });
    }

    @SingleClick
    @Override
    public void onClick(SuperTextView view) {
        int id = view.getId();
        if (id == R.id.menu_setting) {
            openPage(FragmentUpatePasswordVertifyCode.class);
        } else if (id == R.id.menu_logout) {
            showLoginOutDialog();
        }
    }
}
